package com.tiantian.quanminxiu.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.TimerTaskUtils;
import com.tiantian.quanminxiu.utils.gift.BigGiftShowManager;
import com.tiantian.quanminxiu.view.PhoneLookView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.adapter.ChatAdapter;
import com.tiantian.quanminxiu.adapter.UserAvatarAdapter;
import com.tiantian.quanminxiu.api.SocketUtils;
import com.tiantian.quanminxiu.event.LiveRoomEvent;
import com.tiantian.quanminxiu.event.SocketGiftEvent;
import com.tiantian.quanminxiu.event.SocketMsgEvent;
import com.tiantian.quanminxiu.event.SocketStateEvent;
import com.tiantian.quanminxiu.model.ChatMessage;
import com.tiantian.quanminxiu.model.GiftModel;
import com.tiantian.quanminxiu.model.RunwayModel;
import com.tiantian.quanminxiu.model.User;
import com.tiantian.quanminxiu.module.AspectFrameLayout;
import com.tiantian.quanminxiu.presenter.PhoneLookPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.utils.EventBusUtils;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.utils.face.KJChatKeyboard;
import com.tiantian.quanminxiu.utils.gift.GiftShowManager;
import com.tiantian.quanminxiu.utils.gift.RunwayShowManager;
import com.shenglian.utils.utils.CommonUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import tcking.github.com.giraffeplayer.GiraffePlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * Created by admin on 2016/9/5.
 */
public class Activity_PhoneLook extends ActivityBase implements PhoneLookView,KJChatKeyboard.SendMessageListener{

    private GiraffePlayer player;
    private String Live_URL = "";
    private int RoomId;
    private int fan;
    private int Anchor_Level;
    private String sign = "";
    private int mid;
    private String Anchor_NickName = "",Live_Title;
    private boolean Attention;
    private boolean isJoin;

    private View relRootPhoneLook,txtUserAttention,vBackOut;
    private View vInputMark,relInputRoot,linTotalContribution,linAnchorInfo,vPomeloGift;
    private TextView txtUserCount,txtAnchorName,txtBeansCount,txtUserList,txtPomeloNum;
    private ImageView imgBottomChat,imgBottomShare,imgBottomGift,imgAnchorAvatar,imgLiveCover;
    private RecyclerView mRecyclerView,rvUserAvatar;

    private ChatAdapter chatAdapter;
    private UserAvatarAdapter avatarAdapter;
    private LinearLayoutManager layoutManager;
    private List<ChatMessage> chatList;
    private List<User> userList;
    private JSONArray anchorTag;

    private GiftShowManager giftShowManager;
    private BigGiftShowManager bigGiftShowManager;
    private RunwayShowManager serviceShowManager,roomShowManager,serviceGiftShowManager,UserJoinShowManager;
    private KJChatKeyboard chatKeyboard;

    private PhoneLookPresent present;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_look);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);   //应用运行时，保持屏幕高亮，不锁屏
        Init(false);
        EventBusUtils.getInstance().register(this);

        present = new PhoneLookPresent();
        present.attachView(this);

        sign = getIntent().getStringExtra("sign");
        mid = getIntent().getIntExtra("mid", -1);
        RoomId = getIntent().getIntExtra("RoomId",-1);
        if(RoomId<0){
            CommonUtils.showToask(thisContext,"错误的房间号");
            finish();
        }

        InitView();

        setSocketUtils();

        UserData.Reload.Activity_PhoneLook = true;
    }

    //region InitView
    @Override
    public void InitView() {

        txtUserCount = (TextView) findViewById(R.id.txtUserCount);
        txtPomeloNum = (TextView) findViewById(R.id.txtPomeloNum);
        txtAnchorName = (TextView) findViewById(R.id.txtAnchorName);
        txtBeansCount = (TextView) findViewById(R.id.txtBeansCount);
        txtUserList = (TextView) findViewById(R.id.txtUserList);
        relRootPhoneLook = findViewById(R.id.relRootPhoneLook);
        vBackOut = findViewById(R.id.vBackOut);
        vPomeloGift = findViewById(R.id.vPomeloGift);
        vInputMark = findViewById(R.id.vInputMark);
        relInputRoot = findViewById(R.id.relInputRoot);
        linAnchorInfo = findViewById(R.id.linAnchorInfo);
        txtUserAttention = findViewById(R.id.txtUserAttention);
        linTotalContribution = findViewById(R.id.linTotalContribution);
        imgLiveCover = (ImageView) findViewById(R.id.imgLiveCover);
        imgAnchorAvatar = (ImageView) findViewById(R.id.imgAnchorAvatar);
        imgBottomChat = (ImageView) findViewById(R.id.imgBottomChat);
        imgBottomShare = (ImageView) findViewById(R.id.imgBottomShare);
        imgBottomGift = (ImageView) findViewById(R.id.imgBottomGift);
        mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
        rvUserAvatar = (RecyclerView) findViewById(R.id.rvUserAvatar);
        layoutManager = new LinearLayoutManager(thisContext);
        mRecyclerView.setLayoutManager(layoutManager);

        LinearLayoutManager UserAvatarLinearLayout = new LinearLayoutManager(thisContext);
        UserAvatarLinearLayout.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvUserAvatar.setLayoutManager(UserAvatarLinearLayout);

        AspectFrameLayout cameraPreview_afl = (AspectFrameLayout) findViewById(R.id.cameraPreview_afl);
        cameraPreview_afl.setShowMode(AspectFrameLayout.SHOW_MODE.FULL);
        chatKeyboard = (KJChatKeyboard) findViewById(R.id.chatKeyboard);
        RelativeLayout relServiceMessage = (RelativeLayout) findViewById(R.id.relServiceMessage);
        RelativeLayout relRoomMessage = (RelativeLayout) findViewById(R.id.relRoomMessage);
        RelativeLayout relGiftMessage = (RelativeLayout) findViewById(R.id.relGiftMessage);
        RelativeLayout relUserJoinMessage = (RelativeLayout) findViewById(R.id.relUserJoinMessage);
        LinearLayout linGiftMessage = (LinearLayout) findViewById(R.id.linGiftMessage);
        ImageView ifcImageView = (ImageView) findViewById(R.id.ifcImageView);
        serviceShowManager = new RunwayShowManager(thisActivity,relServiceMessage);
        serviceGiftShowManager = new RunwayShowManager(thisActivity,relGiftMessage);
        UserJoinShowManager = new RunwayShowManager(thisActivity,relUserJoinMessage);
        roomShowManager = new RunwayShowManager(thisActivity,relRoomMessage);
        giftShowManager = new GiftShowManager(thisActivity,linGiftMessage);
        bigGiftShowManager = new BigGiftShowManager(thisActivity,ifcImageView);

        RxView.clicks(vInputMark).map(View->vInputMark).subscribe(OC_Botton);
        RxView.clicks(vBackOut).map(View->vBackOut).subscribe(OC_Botton);
        RxView.clicks(imgBottomChat).map(View->imgBottomChat).subscribe(OC_Botton);
        RxView.clicks(imgBottomShare).map(View->imgBottomShare).subscribe(OC_Botton);
        RxView.clicks(imgBottomGift).map(View->imgBottomGift).subscribe(OC_Botton);
        RxView.clicks(vPomeloGift).map(View->vPomeloGift).subscribe(OC_Botton);
        RxView.clicks(linAnchorInfo).map(View->linAnchorInfo).subscribe(OC_Botton);
        RxView.clicks(txtUserAttention).map(View->txtUserAttention).subscribe(OC_Botton);
        RxView.clicks(linTotalContribution).map(View->linTotalContribution).subscribe(OC_Botton);

        chatKeyboard.setSendMessageListener(this);
    }
    //endregion

    //region PageInit
    @Override
    public void PageInit(){
        if(!UserData.Reload.Activity_PhoneLook){
            return;
        }
        UserData.Reload.Activity_PhoneLook = false;

        chatList = new ArrayList<ChatMessage>();
        userList = new ArrayList<User>();
        mRecyclerView.setAdapter(chatAdapter = new ChatAdapter(thisActivity, chatList));
        rvUserAvatar.setAdapter(avatarAdapter = new UserAvatarAdapter(thisActivity, userList));
        txtPomeloNum.setText(UserData.Base.Pomelo+"");
        APPUtils.displayImage(APPUtils.createUserCover(RoomId),imgLiveCover);

        initPlayer();
        txtUserAttention.setVisibility(Attention ? View.GONE : View.VISIBLE);
        Loading_Open();
        sendLiveMessage("Join", RoomId, mid + "", 100, sign);

    };
    //endregion

    //region 初始化播放器 initPlayer
    private void initPlayer(){
        player = new GiraffePlayer(this);
        player.live(true);
        player.onError(new GiraffePlayer.OnErrorListener() {
            @Override
            public void onError(int what, int extra) {

                switch (what) {
                    case IMediaPlayer.MEDIA_ERROR_UNKNOWN:
                        //位置错误
                        break;
                    case IMediaPlayer.MEDIA_ERROR_SERVER_DIED:
                        //服务器错误
                        break;
                }
                switch (extra) {
                    case IMediaPlayer.MEDIA_ERROR_IO:
                        //本地文件或网络相关错误
                        break;
                    case IMediaPlayer.MEDIA_ERROR_MALFORMED:
                        //比特流不符合相关的编码标准和文件规范
                        break;
                    case IMediaPlayer.MEDIA_ERROR_UNSUPPORTED:
                        //框架不支持该功能
                        break;
                    case IMediaPlayer.MEDIA_ERROR_TIMED_OUT:
                        //一些操作超时
                        break;
                }
                play();
            }
        }).onInfo(new GiraffePlayer.OnInfoListener() {
            @Override
            public void onInfo(int what, int extra) {
                switch (what) {
                    case IMediaPlayer.MEDIA_INFO_UNKNOWN:
                        //未知的信息
                        break;
                    case IMediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
                        //视频过于复杂解码太慢
                        break;
                    case IMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
                        imgLiveCover.setVisibility(View.GONE);
                        mHeader.NotDate_Show("");
                        Loading_Close();
                        //开始渲染第一帧
                        break;
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
                        //暂停播放开始缓冲更多数据
                        break;
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
                        //缓冲了足够的数据重新开始播放
                        break;
                    case IMediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
                        //错误交叉
                        break;
                    case IMediaPlayer.MEDIA_INFO_NOT_SEEKABLE:
                        //媒体不能够搜索
                        break;
                    case IMediaPlayer.MEDIA_INFO_METADATA_UPDATE:
                        //一组新的元数据用
                        break;
                    case IMediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE:
                        //不支持字幕
                        break;
                    case IMediaPlayer.MEDIA_INFO_SUBTITLE_TIMED_OUT:
                        //读取字幕使用时间过长
                        break;
                }

                switch (extra) {
                    case IMediaPlayer.MEDIA_INFO_VIDEO_ROTATION_CHANGED:
                        //媒体信息视频旋转改变
                        break;
                    case IMediaPlayer.MEDIA_INFO_AUDIO_RENDERING_START:
                        //媒体开始渲染音频信息
                        break;
                }
            }
        });
    }
    //endregion

    //region 底部按钮事件 OC_Botton
    private Action1<View> OC_Botton =v-> {
        switch (v.getId()){
            case R.id.vInputMark:
                boolean MarkVisibility = relInputRoot.getVisibility()== View.VISIBLE;
                relInputRoot.setVisibility(MarkVisibility?View.GONE:View.VISIBLE);
                chatKeyboard.hideKeyboard(thisContext);
                chatKeyboard.setMessageType(KJChatKeyboard.MSG_CHAT);
                break;
            case R.id.imgBottomChat:
                boolean Visibility = relInputRoot.getVisibility()== View.GONE;
                relInputRoot.setVisibility(Visibility?View.VISIBLE:View.GONE);
                if(Visibility){
                    chatKeyboard.showKeyboard(thisContext);
                }
                break;
            case R.id.imgBottomShare:
                APPUtils.ShareAppUtils(thisActivity, Anchor_NickName, RoomId,Live_Title);
                break;
            case R.id.imgBottomGift:
                APPUtils.Dialog_GiftList_To(thisActivity,relRootPhoneLook,RoomId,Anchor_NickName);
                break;
            case R.id.vPomeloGift:
                if(UserData.Base.Pomelo>0){
                    sendLiveMessage("PomeloPresent", 1);
                }
                break;
            case R.id.linAnchorInfo:
                APPUtils.Dialog_UserInfoCard_To(thisActivity, RoomId,Anchor_NickName,fan,-1,Anchor_Level,Attention);
                break;
            case R.id.linTotalContribution:
                APPUtils.FanRoll_To(thisActivity, RoomId,Anchor_NickName);
                break;
            case R.id.txtUserAttention:
                present.setAttention(RoomId + "");
                break;
            case R.id.vBackOut:
                finish();
                break;

        }
    };
    //endregion

    //region 设置得到柚子计时器 F_setPomeloTimer
    private void F_setPomeloTimer(){
        TimerTaskUtils.getInstance().start(5 * 60 * 1000, new TimerTask() {
            @Override
            public void run() {
                sendLiveMessage("PomeloGet");
            }
        });
    }
    //endregion

    //region 清除得到柚子计时器 F_clearPomeloTimer
    private void F_clearPomeloTimer(){
        TimerTaskUtils.getInstance().destroyed();
    }
    //endregion

    //region onResume onPause onDestroy
    @Override
    protected void onResume() {
        super.onResume();
        PageInit();
        F_setPomeloTimer();
        player.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        F_clearPomeloTimer();
        player.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeActivity();
        player.onDestroy();
        EventBusUtils.getInstance().unregister(this);
        present.detachView();
    }
    //endregion

    //region onBackPressed
    @Override
    public void onBackPressed() {
        if (player != null && player.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
    //endregion

    //region 发送socket消息 sendLiveMessage(String command,Object... args)
    @Override
    public void sendLiveMessage(String command,Object... args) {
        SocketUtils.getInstance().sendMessage(command, args);
    }
    //endregion

    //region socketUtils设置 setSocketUtils
    @Override
    public void setSocketUtils() {
        SocketUtils.getInstance().bindActivity(thisContext);
    }
    //endregion

    //region 清除socketUtils设置 clearSocketUtils
    @Override
    public void clearSocketUtils() {
        SocketUtils.getInstance().distroySocket();
    }
    //endregion

    //region 接受socket状态消息 receiveStateSocket(SocketStateEvent event)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveStateSocket(SocketStateEvent event){
        if(event.getJsonObject()==null&&event.getSocketTyep()>SocketUtils.CONNECTERROR){
            return;
        }
        JSONObject jsonObject = event.getJsonObject();
        switch (event.getSocketTyep()) {
            case SocketUtils.CONNECTERROR:
            case SocketUtils.DISCONNECT:
                //连接socket服务器出错
                //连接socket服务器出错
                Log.i("receiveStateSocket", "--live:DISCONNECT or CONNECTERROR");
                isJoin = false;
                Observable.just("")
                        .delay(1000, TimeUnit.MILLISECONDS)
                        .throttleFirst(1000, TimeUnit.MILLISECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.newThread())
                        .subscribe(str -> {
                            sendLiveMessage("Join", RoomId, UserData.Current.mid + "", Constants.APP_TYPE, UserData.Current.sign);
                        }, throwable -> {
                            CommonUtils.showToask(this.getContext(), throwable.getMessage());
                        });
                break;
            case SocketUtils.CONNECT:
                //连接到socket服务器
                break;
            case SocketUtils.LIVEBROADCASTLINSENER:
                //region 主播状态开播直播监听 LIVEBROADCASTLINSENER
                String LiveType = jsonObject.optString("Type");
                if("Live".equalsIgnoreCase(LiveType)){
                    JSONObject RoomInfo = jsonObject.optJSONObject("RoomInfo");
                    Live_URL = RoomInfo.optString("Live_URL");
                    play();
                }else if("LiveEnd-BlockSet".equalsIgnoreCase(LiveType)){
                    player.stop();
                    anchorNotLive("艺人房间已冻结...");
                }else if(LiveType.contains("LiveEnd")){
                    player.stop();
                    anchorNotLive("艺人结束直播...");
                }
                //endregion
                break;
            case SocketUtils.GUARDIANLINSENER:
                //region 守护购买通知 LIVEBROADCAST
                if(jsonObject.optString("flag").equals("0")){
                    JSONObject guardVaule = jsonObject.optJSONObject("Data");
                    JSONObject RoomUserInfo = guardVaule.optJSONObject("RoomUserInfo");
                    if (RoomUserInfo!=null) {
                        String User_NickName = RoomUserInfo.optString("NickName");
                        ChatMessage OtherJosn = new ChatMessage();
                        OtherJosn.setType(1);
                        OtherJosn.setUserid(RoomUserInfo.optInt("User_ID"));
                        OtherJosn.setUserlLevel(RoomUserInfo.optInt("Level"));
                        OtherJosn.setUsername(User_NickName);
                        OtherJosn.setWelcomeString(Html.fromHtml("恭喜 " + "[level"+RoomUserInfo.optInt("Level")+"] "+"<font color='#ffff83'>" + User_NickName + "</font>" + " 成为守护"));
                        AddChatList(OtherJosn);
                    }
                    CommonUtils.showToask(thisContext,"恭喜你，购买 "+guardVaule.optJSONObject("GuardianInfo").optString("Name")+"守护 成功");
                }else if(jsonObject.optString("flag").equals("-2")){
                    APPUtils.Recharge_TO(thisActivity);
                    CommonUtils.showToask(thisContext,"购买守护失败:"+jsonObject.optString("message"));
                }else {
                    CommonUtils.showToask(thisContext,"购买守护失败:"+jsonObject.optString("message"));
                }
                //endregion
                break;
            case SocketUtils.GUARDIANBROADCASTLINSENER:
                //region 其他守护监听 LIVEBROADCAST
                JSONObject RoomUserInfo = jsonObject.optJSONObject("RoomUserInfo");
                if (RoomUserInfo!=null) {
                    ChatMessage OtherJosn = new ChatMessage();
                    OtherJosn.setType(1);
                    String User_NickName = RoomUserInfo.optString("NickName");
                    OtherJosn.setUserid(RoomUserInfo.optInt("User_ID"));
                    OtherJosn.setUserlLevel(RoomUserInfo.optInt("Level"));
                    OtherJosn.setUsername(User_NickName);
                    OtherJosn.setWelcomeString(Html.fromHtml("恭喜 " + "[level"+RoomUserInfo.optInt("Level")+"] "+"<font color='#ffff83'>" + User_NickName + "</font>" + " 成为守护"));
                    AddChatList(OtherJosn);
                }
                //endregion
                break;
            case SocketUtils.BLACKSETBROADCAST:
                //region 踢人监听 BLACKSETBROADCAST
                //TYPE =1：禁言，2：踢人，3：取消禁言，4：取消踢人
                try{
                    ChatMessage blackMsg = new ChatMessage();
                    blackMsg.setType(1);
                    switch (jsonObject.optInt("Type")){
                        case 1:
                            blackMsg.setWelcomeString(Html.fromHtml("[roomadmin] "+jsonObject.optJSONObject("RoomUserInfo_Admin").optString("NickName")+""+" 设置 "+ jsonObject.optJSONObject("RoomUserInfo").optString("NickName")+ " 禁言"));
                            break;
                        case 2:
                            blackMsg.setWelcomeString(Html.fromHtml("[roomadmin] "+jsonObject.optJSONObject("RoomUserInfo_Admin").optString("NickName")+" 将 "+ jsonObject.optJSONObject("RoomUserInfo").optString("NickName")+ " 踢出房间"));
                            break;
                        case 3:
                            blackMsg.setWelcomeString(Html.fromHtml("[roomadmin] "+jsonObject.optJSONObject("RoomUserInfo_Admin").optString("NickName")+" 解除 "+ jsonObject.optJSONObject("RoomUserInfo").optString("NickName")+ " 禁言"));
                            break;
                        case 4:
                            blackMsg.setWelcomeString(Html.fromHtml("[roomadmin] "+jsonObject.optJSONObject("RoomUserInfo_Admin").optString("NickName")+" 设置 "+ jsonObject.optJSONObject("RoomUserInfo").optString("NickName")+ " 恢复进房"));
                            break;
                    }
                    AddChatList(blackMsg);
                }catch (Exception e){
                    CommonUtils.showToask(thisContext,"解析json数据错误:"+e.getMessage());
                    e.printStackTrace();
                }
                //endregion
                break;
            case SocketUtils.WARNINGSETBROADCAST:
                //region 警告事件监听 WARNINGSETBROADCAST
                JSONObject WarningInfo = jsonObject.optJSONObject("WarningInfo");
                ChatMessage warnMsg = new ChatMessage();
                warnMsg.setType(1);
                warnMsg.setWelcomeString(Html.fromHtml("[officialadmin] <font color='#ffff83'>秩序监管</font>："+WarningInfo.optString("Type")+(WarningInfo.optInt("Integral")>0?"，扣 "+WarningInfo.optInt("Integral")+" 分<br>"+"提醒艺人:"+WarningInfo.optString("Remark")+"</br>":"<br>"+"提醒艺人:"+WarningInfo.optString("Remark")+"</br>")));
                AddChatList(warnMsg);
                //endregion
                break;
            case SocketUtils.OUTBROADCASTLINSENER://
                //region 其他人退出房间监听 OUTBROADCASTLINSENER
                JSONObject outValue = jsonObject.optJSONObject("RoomUserInfo");
                if (outValue!=null) {
                    try{
                        txtUserList.setText((Integer.parseInt(txtUserList.getText().toString())-1)+"");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                //endregion
                break;
            case SocketUtils.JOINBROADCASTLINSENER:
                //region 其他人进房监听 JOINBROADCASTLINSENER
                JSONObject joinValue = jsonObject.optJSONObject("RoomUserInfo");
                if (joinValue!=null) {
                    RunwayModel vo = new RunwayModel(5);
                    String Usernickname = joinValue.optString("NickName");
                    vo.setUserName(Usernickname);
                    vo.setUserlevel(joinValue.optInt("Level"));
                    vo.setRunwayId(joinValue.optString("User_ID"));
                    UserJoinShowManager.addRunWay(vo);
                    UserJoinShowManager.showRunWay();
                    try{
                        txtUserList.setText((Integer.parseInt(txtUserList.getText().toString())+1)+"");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                //endregion
                break;
            case SocketUtils.USERLEVElBROADCASTLINSENER:
                //region 用户升级监听 USERLEVElBROADCASTLINSENER
                JSONObject roomUserInfo = jsonObject.optJSONObject("RoomUserInfo");
                if (roomUserInfo!=null) {
                    String level = roomUserInfo.optString("Level_Name");
                    int uid = roomUserInfo.optInt("User_ID");
                    String nickname = roomUserInfo.optString("NickName");
                    ChatMessage useInfoMsg = new ChatMessage();
                    useInfoMsg.setType(1);
                    useInfoMsg.setUserid(roomUserInfo.optInt("User_ID"));
                    useInfoMsg.setUserlLevel(roomUserInfo.optInt("Level"));
                    useInfoMsg.setUsername(nickname);
                    useInfoMsg.setWelcomeString(Html.fromHtml("恭喜 " +"[level"+roomUserInfo.optInt("Level")+"] "+"<font color='#ffff83'>" + nickname + "</font>" +" 升到 "+ level+" !"));
                    AddChatList(useInfoMsg);
                    if (uid == UserData.Base.User_ID) {
                        UserData.Base.Level = roomUserInfo.optInt("Level");
                    }
                }
                //endregion
                break;
            case SocketUtils.USERLISTLINSENER:
                //region 用户列表 USERLISTLINSENER
                JSONArray arr = jsonObject.optJSONArray("Data");
                userList.clear();
                for (int i=0;i<arr.length();i++){
                    User u = new User();
                    JSONObject value = arr.optJSONObject(i);
                    u.setCoin(value.optInt("Coin"));
                    u.setIsAdmin(value.optBoolean("IsAdmin"));
                    u.setUser_ID(value.optInt("User_ID"));
                    u.setIsRoomGuardian(value.optBoolean("RoomGuardian"));
                    u.setSex(value.optInt("Sex"));
                    u.setLevel(value.optInt("Level"));
                    u.setNickName(value.optString("NickName"));
                    u.setIsAnchor(value.optBoolean("IsAnchor"));
                    userList.add(u);
                    avatarAdapter.notifyDataSetChanged();
                }
                txtUserList.setText(userList.size()+"");
                //当前房间数人数 （未完成）
                //endregion
                break;
            case SocketUtils.GUARDIANLISTLINSENER:
                //region 守护列表 GUARDIANLISTLINSENER
                Log.i("dd","--aaa:"+jsonObject.toString());
                UserData.Current.GuardList = jsonObject.optJSONArray("Data");
                //endregion
                break;
            case SocketUtils.JOINLINSENER:
                //region 发送我的进房信息并监听 JOINLINSENER
                Log.e("joinlinsener",jsonObject.toString());
                if (jsonObject.optString("flag").equals("0")&&!isJoin) {
                    try {
                        isJoin = true;
                        jsonObject = new JSONObject(jsonObject.optString("Data"));
                        UserData.Base.UserPackage = jsonObject.optJSONArray("UserPackage");
                        Attention = jsonObject.optBoolean("Attention");
                        txtUserAttention.setVisibility(Attention?View.GONE:View.VISIBLE);
                        String MountsInfo = jsonObject.optString("MountsInfo");
                        String Gift = jsonObject.optString("Gift");
                        //RoomInfo
                        JSONObject joinRoomInfo = jsonObject.optJSONObject("RoomInfo");
                        int roomid = joinRoomInfo.optInt("RoomID");
                        boolean isLive = joinRoomInfo.optBoolean("Live");
                        Live_URL = joinRoomInfo.optString("Live_URL");
                        Live_Title = joinRoomInfo.optString("Live_Title");

                        JSONObject joinRoomAnchorInfo = jsonObject.optJSONObject("RoomAnchorInfo");
                        anchorTag = joinRoomAnchorInfo.optJSONArray("Tags");
                        String RoomName = joinRoomAnchorInfo.optString("RoomName");
                        String welcomString = joinRoomAnchorInfo.optString("RoomWelcome");
//                            pNum = joinRoomAnchorInfo.optString("Pomelo");//主播柚子数
                        String Anchor_EXP = joinRoomAnchorInfo.optString("EXP");
                        Anchor_Level = joinRoomAnchorInfo.optInt("Level");
                        Anchor_NickName = joinRoomAnchorInfo.optString("NickName");
                        fan = joinRoomAnchorInfo.optInt("Fans");
                        txtUserCount.setText(Live_Title);
                        txtBeansCount.setText(joinRoomAnchorInfo.optString("Beans"));
                        txtAnchorName.setText(Anchor_NickName+"");
                        imgAnchorAvatar.post(new Runnable() {
                            @Override
                            public void run() {
                                APPUtils.displayImage(APPUtils.createUserAvatar(roomid), imgAnchorAvatar,thisActivity, imgAnchorAvatar.getHeight() / 2);
                            }
                        });

                        //RoomUserInfo
                        if(isLive){
                            ChatMessage safeMsg = new ChatMessage();
                            safeMsg.setType(1);
                            safeMsg.setWelcomeString(Html.fromHtml("[officialadmin] 全民秀安全提示：聊天中若涉及财产的操作，冒充官方人员，请联系官方客服核实对方身份，谨防受骗！"));
                            AddChatList(safeMsg);
                        }
                        if (!TextUtils.isEmpty(welcomString)) {
                            ChatMessage welMsg = new ChatMessage();
                            welMsg.setType(1);
                            welMsg.setWelcomeString(Html.fromHtml("欢迎 " + "<font color='#ffff83'>[全民秀直播]: </font>" + welcomString));
                            AddChatList(welMsg);
                        }
                        JSONObject joinRoomUserInfo = new JSONObject(jsonObject.optString("RoomUserInfo"));
                        String joinnickname = joinRoomUserInfo.optString("NickName");
                        if (!TextUtils.isEmpty(joinnickname)) {
                            ChatMessage giftrechargeInfo = new ChatMessage();
                            giftrechargeInfo.setType(1);
                            giftrechargeInfo.setUserid(joinRoomUserInfo.optInt("User_ID"));
                            giftrechargeInfo.setUserlLevel(joinRoomUserInfo.optInt("Level"));
                            giftrechargeInfo.setUsername(joinnickname);
                            giftrechargeInfo.setWelcomeString(Html.fromHtml("欢迎 " + "[level"+joinRoomUserInfo.optString("Level")+"] " + "<font color='#ffff83'>" + joinnickname + "</font>" + " 入场"));
                            AddChatList(giftrechargeInfo);
                        }
                        if (isLive == true) {
                            play();
                        } else {
                            anchorNotLive("艺人不在直播间...");
                        }
                        sendLiveMessage("UserList");//请求用户列表
                        sendLiveMessage("GuardianList");//请求守护列表
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if(!jsonObject.optString("flag").equals("0")){
                    SweetDialog_InitNetworkTips(jsonObject.optString("message"))
                            .setConfirmClickListener(sDialog -> {
                                Loading_Open();
                                sendLiveMessage("Join", RoomId, UserData.Current.mid + "", Constants.APP_TYPE, UserData.Current.sign);
                            });
                    SweetDialog_Show();
                }
                //endregion
                break;
            case SocketUtils.RUNWAYBROADCASTLINSENER:
                //region 全服跑道监听 RUNWAYBROADCASTLINSENER
                String Type= jsonObject.optString("Type");
                if(!Type.isEmpty()&&Type.equalsIgnoreCase("Gift")){
                    JSONObject value = jsonObject.optJSONObject("Data");
                    GiftModel g = new GiftModel();
                    g.setNum(value.optInt("GiftNum"));
                    g.setSendName(value.optJSONObject("RoomUserInfo").optString("NickName"));
                    g.setAnchorName(value.optJSONObject("RoomAnchorInfo").optString("NickName"));
                    g.setGiftID(value.optJSONObject("GiftInfo").optInt("id"));
                    g.setGiftName(value.optJSONObject("GiftInfo").optString("Name"));
                    RunwayModel vo = new RunwayModel(1);
                    vo.setGiftModel(g);
                    vo.setUserlevel(value.optJSONObject("RoomUserInfo").optInt("Level"));
                    vo.setRunwayId(value.optJSONObject("RoomAnchorInfo").optString("NickName") + "#" + value.optString("CreateDate"));
                    serviceGiftShowManager.addRunWay(vo);
                    serviceGiftShowManager.showRunWay();
                } else if(!Type.isEmpty()&&Type.equalsIgnoreCase("Guardian")){
                    JSONObject value = jsonObject.optJSONObject("Data");
                    RunwayModel vo = new RunwayModel(4);
                    vo.setUserName(value.optJSONObject("RoomUserInfo").optString("NickName"));
                    vo.setUserlevel(value.optJSONObject("RoomUserInfo").optInt("Level"));
                    vo.setAnchorlevel(value.optJSONObject("RoomAnchorInfo").optInt("Level"));
                    vo.setAnchorName(value.optJSONObject("RoomAnchorInfo").optString("NickName"));
                    vo.setRunwayId(value.optJSONObject("RoomAnchorInfo").optString("NickName") + "#" + value.optString("CreateDate"));
                    serviceGiftShowManager.addRunWay(vo);
                    serviceGiftShowManager.showRunWay();
                }
                //endregion
                break;
            case SocketUtils.HORNBROADCASTLINSENER:
                //region 喇叭监听(其他) HORNBROADCASTLINSENER
                JSONObject HornInfo = jsonObject.optJSONObject("HornInfo");
                Log.i("dd","--HORNBROADCASTLINSENER:"+jsonObject.toString());
                RunwayModel model;
                if (HornInfo.optString("Mode").equalsIgnoreCase("ROOM")){
                    model = new RunwayModel(2);
                    roomShowManager.addRunWay(model);
                }else {
                    model = new RunwayModel(3);
                    serviceShowManager.addRunWay(model);
                }
                model.setContent(jsonObject.optString("HornMessage"));
                model.setUserName(jsonObject.optJSONObject("RoomUserInfo").optString("NickName"));
                model.setAnchorName(jsonObject.optJSONObject("RoomAnchorInfo").optString("NickName"));
                model.setAnchorId(jsonObject.optJSONObject("RoomAnchorInfo").optInt("RoomID"));
                model.setUserId(jsonObject.optJSONObject("RoomUserInfo").optInt("User_ID"));
                model.setRunwayId(jsonObject.optJSONObject("RoomAnchorInfo").optInt("User_ID") + "#" + System.currentTimeMillis());
                if (HornInfo.optString("Mode").equalsIgnoreCase("ROOM")){
                    roomShowManager.showRunWay();
                }else {
                    serviceShowManager.showRunWay();
                }
                //endregion
                break;
            case SocketUtils.HORNLINSENER:
                //region 喇叭监听(我的) HORNLINSENER
                Log.i("dd","--HORNLINSENER:"+jsonObject.toString());
                if (jsonObject.optString("flag").equals("0")) {
                    jsonObject = jsonObject.optJSONObject("Data");
                    JSONObject HornValue = jsonObject.optJSONObject("HornInfo");
                    if (HornValue.optString("Mode").equalsIgnoreCase("ROOM")){
                        model = new RunwayModel(2);
                        roomShowManager.addRunWay(model);
                        model.setContent(jsonObject.optString("HornMessage"));
                        model.setUserName(jsonObject.optJSONObject("RoomUserInfo").optString("NickName"));
                        model.setAnchorName(jsonObject.optJSONObject("RoomAnchorInfo").optString("NickName"));
                        model.setAnchorId(jsonObject.optJSONObject("RoomAnchorInfo").optInt("RoomID"));
                        model.setUserId(jsonObject.optJSONObject("RoomUserInfo").optInt("User_ID"));
                        model.setRunwayId(jsonObject.optJSONObject("RoomAnchorInfo").optInt("User_ID") + "#" + System.currentTimeMillis());
                        roomShowManager.showRunWay();
                    }
                } else if(jsonObject.optString("flag").equals("-2")){
                    CommonUtils.showToask(thisContext, jsonObject.optString("message"));
                    APPUtils.Recharge_TO(thisActivity);
                } else {
                    CommonUtils.showToask(thisContext,"发送失败:"+jsonObject.optString("message"));
                }
                break;
            //endregion
        }
    }
    //endregion

    //region 接受socket礼物消息 receiveGiftSocket(SocketGiftEvent event)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveGiftSocket(SocketGiftEvent event){
        if(event.getJsonObject() == null) {
            return;
        }
        Log.i("receiveMessage", "--receiveMessage:" + event.getJsonObject().toString());
        JSONObject jsonObject = event.getJsonObject();
        switch (event.getSocketTyep()) {
            case SocketUtils.POMELOGETLINSENER:
                //region 我领取星星监听 POMELOGETLINSENER
                // 状态 0:正常 -1:未登录 1:领取太频繁 2:今天已领取完了
                String flag = jsonObject.optString("flag");
                if ("0".equals(flag)) {
                    JSONObject jsonPomelo = jsonObject.optJSONObject("Data");
                    if(jsonPomelo!=null){
                        int Pomelo = jsonPomelo.optInt("Pomelo");
                        UserData.Base.Pomelo = Pomelo;
                        txtPomeloNum.setText(UserData.Base.Pomelo+"");
                    }
                } else if("2".equals(flag)){
                    TimerTaskUtils.getInstance().destroyed();
                }
                //endregion
                break;
            case SocketUtils.POMELOPRESENTLINSENER:
                //region 我送星星 POMELOPRESENTLINSENER
                if (jsonObject.optString("flag").equals("0")) {
                    JSONObject jsonPresent = jsonObject.optJSONObject("Data");
                    if(jsonPresent!=null){
                        JSONObject RoomUserInfo = jsonPresent.optJSONObject("RoomUserInfo");
                        JSONObject RoomAnchorInfo = jsonPresent.optJSONObject("RoomAnchorInfo");
                        if(RoomUserInfo!=null){
                            //更改星星数
                            UserData.Base.Pomelo = RoomUserInfo.optInt("Pomelo");
                            txtPomeloNum.setText(UserData.Base.Pomelo+"");
                        }
                        String Sendername = RoomUserInfo.optString("NickName");
                        ChatMessage pomelopGiftInfo = new ChatMessage();
                        pomelopGiftInfo.setType(4);
                        pomelopGiftInfo.setUserid(RoomUserInfo.optInt("User_ID"));
                        pomelopGiftInfo.setUserlLevel(RoomUserInfo.optInt("Level"));
                        pomelopGiftInfo.setUsername(Sendername);
                        pomelopGiftInfo.setGiftName("星星");
                        pomelopGiftInfo.setGiftCount(jsonPresent.optInt("Num"));
                        pomelopGiftInfo.setReceiverUserName(Anchor_NickName);
                        AddChatList(pomelopGiftInfo);
                    }
                } else {
                    CommonUtils.showToask(thisContext, jsonObject.optString("message"));
                }
                //endregion
                break;
            case SocketUtils.POMELOPRESENTBROADCASTLINSENER:
                //region 其他人送星星监听 POMELOPRESENTBROADCASTLINSENER
                JSONObject RoomAnchorInfo = jsonObject.optJSONObject("RoomAnchorInfo");
                JSONObject jsonUserInfo = jsonObject.optJSONObject("RoomUserInfo");
                String Sendername = jsonUserInfo.optString("NickName");
                ChatMessage pomelopMsg = new ChatMessage();
                pomelopMsg.setType(4);
                pomelopMsg.setUserid(jsonUserInfo.optInt("User_ID"));
                pomelopMsg.setUserlLevel(jsonUserInfo.optInt("Level"));
                pomelopMsg.setUsername(Sendername);
                pomelopMsg.setGiftName("星星");
                pomelopMsg.setGiftCount(jsonObject.optInt("Num"));
                pomelopMsg.setReceiverUserName(Anchor_NickName);
                AddChatList(pomelopMsg);
                //endregion
                break;
            case SocketUtils.GIFTLINSENER:
                //region 我发送礼物监听 GIFTLINSENER
                if (jsonObject.optString("flag").equals("0")) {
                    try {
                        jsonObject = new JSONObject(jsonObject.optString("Data"));
                        JSONObject GiftInfo = jsonObject.optJSONObject("GiftInfo");
                        int Gift_ID = GiftInfo.optInt("id");
                        int Num = jsonObject.optInt("GiftNum");
                        JSONObject giftRoomUserInfo = jsonObject.optJSONObject("RoomUserInfo");
                        String sendername = giftRoomUserInfo.optString("NickName");
                        UserData.Base.Coin = giftRoomUserInfo.optInt("Coin");
                        UserData.Base.Beans = giftRoomUserInfo.optInt("Beans");
                        UserData.Reload.Fragment_Mine = true;
                        UserData.Base.UserPackage = jsonObject.optJSONArray("UserPackage");
                        JSONObject giftRoomAnchorInfo = jsonObject.optJSONObject("RoomAnchorInfo");
                        String receivername = giftRoomAnchorInfo.optString("NickName");
                        GiftModel gift = new GiftModel();
                        gift.setShowId(giftRoomUserInfo.optString("User_ID") + "_" + Gift_ID+"_"+Num);
                        gift.setUser_ID(giftRoomUserInfo.optInt("User_ID"));
                        gift.setGiftName(GiftInfo.optString("Name"));
                        gift.setPrice(GiftInfo.optInt("Price"));
                        gift.setCount(jsonObject.optInt("GiftCount"));
                        gift.setNum(Num);
                        gift.setGiftImg(APPUtils.createGiftImg(Gift_ID));
                        gift.setGiftType(jsonObject.optInt("GiftStreamer") == 0 ? 0 : 1);
                        gift.setSendName(sendername);
                        gift.setAnchorName(receivername);
                        if (giftShowManager != null) {
                            giftShowManager.addGift(gift);
                            giftShowManager.showGift();
                        }

                        if (bigGiftShowManager!=null&&judgeBigGiftName(gift.getGiftName()))
                        {
                            bigGiftShowManager.addGiftWay(gift);
                        }

                        ChatMessage giftpMsg = new ChatMessage();
                        giftpMsg.setType(4);
                        giftpMsg.setUserid(giftRoomUserInfo.optInt("User_ID"));
                        giftpMsg.setUserlLevel(giftRoomUserInfo.optInt("Level"));
                        giftpMsg.setUsername(giftRoomUserInfo.optString("NickName"));
                        giftpMsg.setGiftName(GiftInfo.optString("Name"));
                        giftpMsg.setGiftCount(Num);
                        giftpMsg.setGiftId(Gift_ID);
                        giftpMsg.setReceiverUserName(Anchor_NickName);
                        AddChatList(giftpMsg);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    CommonUtils.showToask(thisContext, jsonObject.optString("message"));
                }
                //endregion
                break;
            case SocketUtils.GIFTBROADCASTLINSENER:
                //region 其他人送礼物监听 GIFTBROADCASTLINSENER
                int Num = jsonObject.optInt("GiftNum");
                JSONObject GiftInfo = jsonObject.optJSONObject("GiftInfo");
                int Gift_ID = GiftInfo.optInt("id");

                JSONObject giftRoomUserInfo = jsonObject.optJSONObject("RoomUserInfo");
                JSONObject giftRoomAnchorInfo = jsonObject.optJSONObject("RoomAnchorInfo");

                String sendername = giftRoomUserInfo.optString("NickName");
                String receivername = giftRoomAnchorInfo.optString("NickName");
                GiftModel gift = new GiftModel();
                gift.setShowId(giftRoomUserInfo.optString("User_ID") + "_" + Gift_ID + "_" + Num);
                gift.setUser_ID(giftRoomUserInfo.optInt("User_ID"));
                gift.setGiftName(GiftInfo.optString("Name"));
                gift.setPrice(GiftInfo.optInt("Price"));
                gift.setCount(jsonObject.optInt("GiftCount"));
                gift.setNum(Num);
                gift.setGiftType(jsonObject.optInt("GiftStreamer")==0?0:1);
                gift.setGiftImg(APPUtils.createGiftImg(Gift_ID));
                gift.setSendName(sendername);
                gift.setAnchorName(receivername);
                if (giftShowManager != null) {
                    giftShowManager.addGift(gift);
                    giftShowManager.showGift();
                }
                if (bigGiftShowManager!=null&&judgeBigGiftName(gift.getGiftName()))
                {
                    bigGiftShowManager.addGiftWay(gift);
                }

                ChatMessage giftpMsg = new ChatMessage();
                giftpMsg.setType(4);
                giftpMsg.setUserid(giftRoomUserInfo.optInt("User_ID"));
                giftpMsg.setUserlLevel(giftRoomUserInfo.optInt("Level"));
                giftpMsg.setUsername(giftRoomUserInfo.optString("NickName"));
                giftpMsg.setGiftName(GiftInfo.optString("Name"));
                giftpMsg.setGiftCount(Num);
                giftpMsg.setGiftId(Gift_ID);
                giftpMsg.setReceiverUserName(Anchor_NickName);
                AddChatList(giftpMsg);
                //endregion
                break;
            case SocketUtils.PONDBROADCASTLINSENER:
                //region 直播间奖池监听(未完成) GIFTBROADCASTLINSENER

                //endregion
                break;

        }
    }
    //endregion

    //region 接受socket聊天消息 receiveMsgSocket(SocketMsgEvent event)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveMsgSocket(SocketMsgEvent event) {
        if(event.getJsonObject()==null){
            return;
        }
        Log.i("receiveMessage", "--receiveMessage:" + event.getJsonObject().toString());
        JSONObject jsonObject = event.getJsonObject();
        switch (event.getSocketTyep()) {
            case SocketUtils.MESSAGELINSENER:
                //region 我发送信息监听 MESSAGELINSENER
                if (jsonObject.optString("flag").equals("0")) {
                    jsonObject = jsonObject.optJSONObject("Data");

                    boolean isprivate = jsonObject.optBoolean("Private");
                    String messageString = jsonObject.optString("Message");
                    JSONObject senderjson = jsonObject.optJSONObject("RoomUserInfo");

                    // String receiverjson = jsonObject.optString("Receiver");
                    String senderName = senderjson.optString("NickName");
                    if (jsonObject.optJSONObject("RoomUserInfo_Receiver")!=null) {
                        // 私聊
                        JSONObject RoomUserInfo_Receiver = jsonObject.optJSONObject("RoomUserInfo_Receiver");
                        String receiver = RoomUserInfo_Receiver.optString("NickName");
                        ChatMessage SendInfo = new ChatMessage();
                        SendInfo.setType(3);
                        if(senderjson.optInt("User_ID")==RoomId){
                            SendInfo.setAnchorLevel(Anchor_Level);
                        }
                        SendInfo.setUserlLevel(senderjson.optInt("Level"));
                        if(isprivate){
                            SendInfo.setMsgContent(Html.fromHtml("对 " + "<font color='#ffff83'>" +  receiver + "</font>" + " 悄悄说：" + messageString));
                        }else {
                            SendInfo.setMsgContent(Html.fromHtml("对 " + "<font color='#ffff83'>" +  receiver + "</font>" + " 说：" + messageString));
                        }
                        SendInfo.setUsername(senderName);
                        AddChatList(SendInfo);
                    }else {
                        ChatMessage MesValue = new ChatMessage();
                        MesValue.setType(3);
                        if(senderjson.optInt("User_ID")==RoomId){
                            MesValue.setAnchorLevel(Anchor_Level);
                        }
                        MesValue.setUserlLevel(senderjson.optInt("Level"));
                        MesValue.setMsgContent(Html.fromHtml(messageString));
                        MesValue.setUsername(senderName);
                        MesValue.setUserid(senderjson.optInt("User_ID"));
                        AddChatList(MesValue);
                    }

                }else {
                    CommonUtils.showToask(thisContext,"发送失败:"+jsonObject.optString("message"));
                }
                //endregion
                break;
            case SocketUtils.MESSAGEBROADCASTLINSENER:
                //region 其他人发送信息监听 MESSAGEBROADCASTLINSENER
                boolean isprivate = jsonObject.optBoolean("Private");
                String messageString = jsonObject.optString("Message");
                JSONObject senderjson = jsonObject.optJSONObject("RoomUserInfo");
                JSONObject RoomUserInfo_Receiver = jsonObject.optJSONObject("RoomUserInfo_Receiver");
                // String receiverjson = jsonObject.optString("Receiver");
                String senderName = senderjson.optString("NickName");
                if (jsonObject.optJSONObject("RoomUserInfo_Receiver")!=null) {
                    // 私聊
                    String receiver = RoomUserInfo_Receiver.optString("NickName");
                    ChatMessage SendInfo = new ChatMessage();
                    SendInfo.setType(3);
                    if(senderjson.optInt("User_ID")==RoomId){
                        SendInfo.setAnchorLevel(Anchor_Level);
                    }
                    SendInfo.setUserlLevel(senderjson.optInt("Level"));
                    if(isprivate){
                        SendInfo.setMsgContent(Html.fromHtml("对 " + "<font color='#ffff83'>" +  receiver + "</font>" + " 悄悄说：" + messageString));
                    }else {
                        SendInfo.setMsgContent(Html.fromHtml("对 " + "<font color='#ffff83'>" +  receiver + "</font>" + " 说：" + messageString));
                    }
                    SendInfo.setUsername(senderName);
                    AddChatList(SendInfo);
                } else {
                    // 公共聊天
                    if (!TextUtils.isEmpty(senderName)) {
                        ChatMessage SendInfo = new ChatMessage();
                        SendInfo.setType(3);
                        if(senderjson.optInt("User_ID")==RoomId){
                            SendInfo.setAnchorLevel(Anchor_Level);
                        }
                        SendInfo.setUserlLevel(senderjson.optInt("Level"));
                        SendInfo.setMsgContent(Html.fromHtml(messageString));
                        SendInfo.setUserid(senderjson.optInt("User_ID"));
                        SendInfo.setUsername(senderName);
                        AddChatList(SendInfo);
                    }
                }
                //endregion
                break;
        }
    }
    //endregion

    //region 接受socket聊天消息 receiveMsgSocket(SocketMsgEvent event)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveLiveMsg(LiveRoomEvent event) {
        if(event.getType()==null){
            return;
        }
        switch (event.getType()){
            case "attentionAchor":
                if(event.getContent().equalsIgnoreCase("false")){
                    txtUserAttention.setVisibility(View.VISIBLE);
                    Attention = false;
                }else {
                    txtUserAttention.setVisibility(View.GONE);
                    Attention = true;
                }
                break;
            case "toldHim":
                setMessageType(KJChatKeyboard.MSG_TOLDHIM,event.getReceiveID(),event.getReceiveName());
                break;
            case "privateChat":
                setMessageType(KJChatKeyboard.MSG_PRIVATE,event.getReceiveID(),event.getReceiveName());
                break;
        }
    }
    //endregion

    //region 添加聊天信息 AddChatList
    @Override
    public void AddChatList(ChatMessage msg){
        chatAdapter.addMessage(msg);
        chatAdapter.notifyDataSetChanged();
        F_scrollToLast();
    }
    //endregion

    //region 跳转到ListView底部 F_scrollToLast（）
    private void F_scrollToLast() {
        layoutManager.scrollToPositionWithOffset(chatAdapter.getItemCount() - 1, 0);
    }
    //endregion

    //region 开始播放 play
    @Override
    public void play(){
        if (!TextUtils.isEmpty(Live_URL) && !Live_URL.equals("null")) {
//            if (Live_URL.contains("rtmp")){
//                Live_URL = Live_URL.replace("rtmp","http")+".flv";
//            }
            player.play(Live_URL);
        }
    }
    //endregion

    //region 用户关注 setAttention
    @Override
    public void setAttention() {
        txtUserAttention.setVisibility(View.GONE);
        Attention = true;
    }
    //endregion

    //region 退出直播间 closeActivity
    @Override
    public void closeActivity() {
        clearSocketUtils();
    }
    //endregion

    @Override
    public void setMessageType(int type, int User_ID, String UserName) {
        chatKeyboard.setMessageType(type,User_ID,UserName);
        relInputRoot.setVisibility(View.VISIBLE);
    }


    //region 艺人不在直播 anchorNotLive
    public void anchorNotLive(String detail){
        Loading_Close();
        mHeader.NotDate_Show(detail);
        imgLiveCover.setVisibility(View.VISIBLE);
        APPUtils.Dialog_NotLive_TO(thisActivity,this);
    }
    //endregion

    // region 房间冻结 roomBoock
    public void roomBoock(){

    }
    //endregion

    //region 按回退键退出 onKeyDown(int keyCode, KeyEvent event)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    //endregion

    //region 键盘发送聊天消息 sendChatMessage
    @Override
    public void sendChatMessage(String type,String content) {
        sendLiveMessage(type, content);
    }

    @Override
    public void sendChatMessage(String type, int User_ID, String content, boolean pre) {
        sendLiveMessage(type,content,User_ID,pre);
    }

    @Override
    public void sendChatMessage(String type, String horn, String content) {
        sendLiveMessage(type,horn,content);
    }

    @Override
    public void switchRoom(int RoomId) {
        UserData.Reload.Activity_PhoneLook = true;
        isJoin = false;
        this.RoomId = RoomId;
        PageInit();
    }
    //endregion

    public boolean judgeBigGiftName(String name){
        switch (name){
            case "跑车":
            case "飞机":
            case "航母":
            case "别墅":
            case "游艇":
                return true;
        }
        return false;
    }
}
